<template>
  <div class="loginWrap">
    
      <el-page-header @back="goBack" content=""></el-page-header>
    <el-form label-width="80px">
      <el-form-item label="用户名:">
        <el-input v-model="userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label=" 邮箱:">
        <text class="iconfont icon-WeChat"></text>
        <el-input v-model="userEmail"></el-input>
      </el-form-item>
      <el-button @click="login" type="primary">登录</el-button>
      <el-button @click="regist" type="primary">注册</el-button>
    </el-form>
    
    
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      userName: "",
      userEmail: ""
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    getImg() {
      let math = { id: Math.floor(Math.random() * 5) };
      console.log(math);
      axios({
        url: "http://localhost:3000/detail/getuserimg",
        method: "post",
        data: math
      }).then(response => {
        this.userImg = response.data.message.userImg;
        localStorage.userImg = this.userImg;
      });
    },
    login() {
      axios({
        url: "http://localhost:3000/detail/login",
        method: "post",
        data: {
          userName: this.userName,
          userEmail: this.userEmail
        }
      }).then(response => {
        if (response.data.message == "登录成功") {
          localStorage.userName = this.userName;
          this.getImg();
          this.$router.go(-1);
        } else if (response.data.message == "这是管理员") {
          this.$message.success("欢迎管理员回来！")
          this.$router.replace("/admin");
        } else {
          this.$message.success("请先注册")
        }
      });
    },
    regist() {
      axios({
        url: "http://localhost:3000/detail/regist",
        method: "post",
        data: {
          userName: this.userName,
          userEmail: this.userEmail
        }
      }).then(response => {
        this.getImg();
        localStorage.userName = this.userName;
        this.$router.go(-1);
      });
    },
  }
};
</script>

<style>
.el-form{
  width: 50%;
  margin: 0 auto;
}
.el-form-item__label{
  color: black ;
}
</style>